package cn.newtouch.pet.controller;

import cn.newtouch.basic.annotation.PreAuthorize;
import cn.newtouch.basic.util.JsonResult;
import cn.newtouch.basic.util.PageList;
import cn.newtouch.pet.domain.Pet;
import cn.newtouch.pet.query.PetQuery;
import cn.newtouch.pet.service.IPetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
@Api(value = "宠物的API",description="宠物相关的CRUD功能")
public class PetController {

    @Autowired
    private IPetService petService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有宠物" )
    @PreAuthorize(name = "所有宠物",sn = "pet:getAll")
    public List<Pet> findAll(){
        return petService.loadAll();
    }
    /**
     * 查询一个
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "通过ID查询宠物",sn = "pet:get")
    public Pet findOne(@PathVariable Long id){
       return petService.loadById(id);
    }

    /**
     * 删除
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(name = "删除宠物",sn = "pet:delete")
    public JsonResult deleteById(@PathVariable("id") Long id){
        try {
            petService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 添加修改
     * @return
     */
    @PutMapping
    @ApiOperation(value = "没有参数ID是添加或者有ID是修改" )
    @PreAuthorize(name = "保存宠物",sn = "pet:save")
    public JsonResult updateOrAdd(@RequestBody Pet pet){
        try {
            if(pet.getId()==null){
                //添加操作
                petService.save(pet);
            }else{
                //修改操作
                petService.update(pet);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 分页
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    @PreAuthorize(name = "宠物分页查询",sn = "pet:list")
    public PageList<Pet> queryPage(@RequestBody PetQuery query){
        try {
            return petService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }

    }

    /**
     * 批量删除
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    @PreAuthorize(name = "宠物批量删除",sn = "pet:patchDel")
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            petService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 上架
     * @return
     */
    @PostMapping("/onsale")
    @ApiOperation(value = "上架" )
    public JsonResult onSale(@RequestBody List<Long> ids){
        try {
            petService.onSale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 下架
     * @return
     */
    @PostMapping("/offsale")
    @ApiOperation(value = "下架" )
    public JsonResult offSale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }



}
